package org.jing.ext.jdbc.mybatis.test.domain;

import org.jing.ext.jdbc.JingJdbcException;
import org.jing.ext.jdbc.mybatis.BaseMyBatisDomain;
import org.jing.ext.jdbc.mybatis.param.Params;
import org.jing.ext.jdbc.mybatis.test.dto.UserDto;
import org.jing.ext.jdbc.mybatis.test.mapper.UserDao;

import java.util.Date;

/**
 * Description: <br>
 *
 * @author bks <br>
 * @since 2022-05-04 <br>
 */
public class UserDomain extends BaseMyBatisDomain {
    public UserDto queryUserById(int id) throws JingJdbcException {
        UserDto dto = getMapperByNewSession(true, UserDao.class).queryUserById(id);
        releaseTopSession();
        return dto;
    }

    public void enableTable() {
        getMapperByCurrentSession(UserDao.class).enableTable();
    }

    public void createUser(String name, String password, String email, String tel) throws JingJdbcException {
        UserDao dao = getMapperByCurrentSession(UserDao.class);
        Params params = new Params()
                .set("id", dao.qryNextId())
                .set("name", name)
                .set("state", "A")
                .set("password", password)
                .set("email", email)
                .set("tel", tel)
                .set("insertDate", new Date());
        dao.createUser(params);
    }
}
